package com.guandata.cdc.helper;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

public class ParamsHelper {
    private static final String TEMP_TABLE_TEMPLATE = "%s_%s";
    private static final String DELIMITER = ",";
    private static final char PROGRAM_CONNECT = '_';
    private static final char SYNTAX_CONNECT = '-';

    public static String getInnerName(String name) {
        if(Objects.isNull(name)) return null;
        // '-' shouldn't be used in Flink SQL table name
        return name.replace(SYNTAX_CONNECT, PROGRAM_CONNECT);
    }

    public static String getInnerTableName(String tableName) {
        var tempTableId = UUID.randomUUID().toString();
        return getInnerName(String.format(TEMP_TABLE_TEMPLATE, tableName, tempTableId));
    }

    /**
     * Get list from string parameter
     *
     * <pre>{@code
     * getListFromString("a, b, c")
     * }</pre>
     *
     * trim parameter and filter out blanks
     *
     * @param listAsString like a, b, c
     * @return a,b,c
     */
    public static List<String> getListFromString(String listAsString) {
        if(Objects.isNull(listAsString)) return List.of();
        return Arrays.stream(listAsString.split(DELIMITER))
            .map(String::trim)
            .filter(s -> !s.isEmpty())
            .collect(Collectors.toList());
    }
}
